
package com.ifcar.app.activity.common;

import com.ifcar.app.activity.CommonFrameActivity;
import com.ifcar.app.activity.newspage.NewsCommentFragment;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * 跟帖模块<br/>
 * 
 * @author wjying
 */
public class CommentWithTitleFragment extends CommentFragment {

    public static final String SHOW_GO_NEWS = "show_go_news";
    
    private View mNewsView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.common_comment_with_title_layout, container, false);
    }

    @Override
    public void onViewCreatedNR(View view, Bundle savedInstanceState) {
        super.onViewCreatedNR(view, savedInstanceState);

        View title = getTitleView();
        if (title != null) {
            View goBackView = view.findViewById(R.id.go_back_common_btn);
            goBackView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (getParent() != null) {
                        ((NewsCommentFragment) getParent()).goback();
                    }
                }
            });
        }
        
        View titleView = getTitleView();
        if (title != null) {

            boolean showGoNews = getArguments() != null
                    && getArguments().getBoolean(SHOW_GO_NEWS, false);
            if (showGoNews) {
                ViewGroup extraLayout = (ViewGroup) titleView
                        .findViewById(R.id.top_right_extra_container);
                LayoutInflater inflater = LayoutInflater.from(getActivity());
                mNewsView = inflater.inflate(R.layout.top_button_layout, extraLayout,
                        false);
                extraLayout.addView(mNewsView);
                
                Button newsBtn = (Button)mNewsView.findViewById(R.id.top_button);
                newsBtn.setText(R.string.original_button);
                newsBtn.setOnClickListener(new OnClickListener() {
                    
                    @Override
                    public void onClick(View v) {
                        if (getParent() != null && getParent() instanceof NewsCommentFragment) {
                            ((NewsCommentFragment)getParent()).showNews();
                        }
                        
                    }
                });
                
            }
        }
    }

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        Context context = getActivity();

        View content = getView().findViewById(R.id.common_comment_frame);
        themeSettingsHelper.setViewBackgroudColor(context, content, R.color.main_bg_color);

        View titleView = getTitleView();

        if (titleView != null) {
            CommonFrameActivity.applyThemeForDefaultTitleView(context, themeSettingsHelper,
                    titleView);
        }
        
        if (mNewsView != null) {
            Button newsBtn = (Button) mNewsView.findViewById(R.id.top_button);
            themeSettingsHelper.setViewBackgroud(context, newsBtn, R.drawable.topcomment_column);
            themeSettingsHelper.setTextViewColor(context, newsBtn, R.color.top_image_title_color);
        }

    }

}
